﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;

using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using Cn.Edwon.Web.Support;
using Cn.Cellula.Model;
using Cn.Cellula.Biz.Port;
using Cn.Edwon.Spring;
using Cn.Edwon.Web;
using Cn.Cellula.Biz;
using System.Collections.Generic;
using Cn.Cellula.Model.Product;
using Cn.Cellula.Client;
using Cn.Cellula.Model.System;
using Cn.Edwon.Web.Validation;
using System.Reflection;
using Cn.Edwon.Biz;
using Cn.Cellula.Model.Security;
using Cn.Cellula.Model.Security.Power;
namespace Cn.Cellula.Website.Admin.Content.Atl {
    public partial class EditAtl : PageSupport {
        IItemBiz biz = BizFactory.Get<IItemBiz>();
        String AtlKey = null;
        protected void Page_Load(object sender, EventArgs e) {
            AtlKey = Request.QueryString["id"];
            if (!Page.IsPostBack) {
                InitForm();
                if (!String.IsNullOrEmpty(AtlKey)) {
                    FillForm(AtlKey);
                }
                String path = BizFactory.Get<ICategoryBiz>().LoadCategoryPath(this.OwnerKey, CodeTypeDef.Category.Product, this.hid_CategoryKey.Value, " / ");
                if (!String.IsNullOrEmpty(path)) {
                    this.txt_CategoryName.Text = path;
                }
            }
        }

        private void InitForm() {
            ClientBiz.BindCodeList(ref this.ddl_FieldType, CodeTypeDef.FormField.Key, true, null);
            ClientBiz.BindCodeList(ref this.ddl_CountRule, CodeTypeDef.CountRule.Key, false,"");
            if (!String.IsNullOrEmpty(Request.QueryString["cid"])) {
                try {
                    this.txt_CategoryName.Text = BizFactory.Get<ICategoryBiz>().LoadCategoryPath(this.OwnerKey, null, Request.QueryString["cid"], " / ");
                    this.hid_CategoryKey.Value = Request.QueryString["cid"];
                } catch { }
            }

            IUserBiz ubiz = BizFactory.Get<IUserBiz>();
            IList<TSysRoleEO> list = ubiz.LoadAllRoles(this.Power.RoleLevel);
            this.ddl_EditableRole.DataSource = list;
            this.ddl_EditableRole.DataBind();
            this.ddl_EditableRole.Items.Insert(0, new ListItem("", ""));
            this.ddl_VisiableRole.DataSource = list;
            this.ddl_VisiableRole.DataBind();
            this.ddl_VisiableRole.Items.Insert(0, new ListItem("", ""));
        }

        /// <summary>
        /// 表单初始化数据
        /// </summary>
        /// <param name="typeKey"></param>
        private void FillForm(String typeKey) {
            VSysAtlEO eo = biz.Load<VSysAtlEO>(typeKey);
            this.ddl_FieldType.SelectedValue = eo.FieldType;
            this.txt_AtlName.Text = eo.AtlName;
            this.txt_OrderNumber.Text = eo.OrderNumber.ToString();
            this.txt_DefaultVaue.Text = eo.DefaultValue;
            this.hid_CategoryKey.Value = eo.CategoryKey;
            this.txt_CategoryName.Text = eo.CategoryName;
            this.ral_Queriable.SelectedValue = eo.IsQueriable + "";
            this.rbl_Report.SelectedValue = eo.IsReport + "";
            this.ddl_CountRule.SelectedValue = eo.CountRule;
            try {
                TSysCategoryEO ceo = BizFactory.Get<IBiz>().Load<TSysCategoryEO>(eo.RefKey);
                this.hid_RefCategoryKey.Value = ceo.CategoryId;
                this.txt_RefCategoryName.Text = ceo.CategoryName;
            } catch (Exception ex) {
                log.Error("获取属性模板的外联对象分类时出现异常：" + ex.Message);
            }
            this.txt_Length.Text = eo.CharLength+"";

            IUserBiz ubiz = BizFactory.Get<IUserBiz>();
            try {
                TSysPowerEO peo = ubiz.Load<TSysPowerEO>(eo.AtlKey);
                if (peo != null) {
                    this.ddl_EditableRole.SelectedValue = peo.EditableRoleKey;
                    this.ddl_VisiableRole.SelectedValue = peo.VisiableRoleKey;
                }
            } catch { }
        }

        /// <summary>
        /// 保存
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btn_save_Click(object sender, EventArgs e) {
            Boolean bk = QuickValidator.Execute(Request, this.txt_AltDesc.Parent);
            if (!bk) {
                log.Error(QuickValidator.Message);
                SiteInfo.Send(Page,"",QuickValidator.Message);
                return;
            }
            IUserBiz ubiz = BizFactory.Get<IUserBiz>();
            //---- 增加新项 ---------------------------------
            if (String.IsNullOrEmpty(AtlKey)) {
                TSysAtlEO eo = new TSysAtlEO();
                eo.AtlDesc = this.txt_AltDesc.Text;
                eo.AtlName = this.txt_AtlName.Text;
                eo.CategoryKey = this.hid_CategoryKey.Value;
                eo.DefaultValue = this.txt_DefaultVaue.Text;
                eo.FieldType = this.ddl_FieldType.SelectedValue;
                eo.IsQueriable = Convert.ToInt32(this.ral_Queriable.SelectedValue);
                eo.IsReport = Convert.ToInt32(this.rbl_Report.SelectedValue);
                eo.RefKey = this.hid_RefCategoryKey.Value;
                eo.CountRule = this.ddl_CountRule.SelectedValue;
                try {
                    eo.CharLength = Convert.ToInt32(this.txt_Length.Text);
                } catch { }
                try {
                    eo.OrderNumber = Convert.ToInt32(this.txt_OrderNumber.Text);
                } catch { }
                String newKey = biz.Save<TSysAtlEO>(eo).ToString();
                ubiz.BindPower(newKey, this.ddl_VisiableRole.SelectedValue, this.ddl_EditableRole.SelectedValue,null, this.Actor.ID);
                BizFactory.Get<ILogBiz>().LogInsert("属性模板", eo.AtlName, this.Actor, Request);
                Response.Redirect("List.aspx?cid=" + this.hid_CategoryKey.Value + "&t=" + AtlKey);
            } else {//更新----------------------------------
                TSysAtlEO eo = biz.Load<TSysAtlEO>(AtlKey);
                eo.AtlDesc = this.txt_AltDesc.Text;
                eo.AtlName = this.txt_AtlName.Text;
                eo.CategoryKey = this.hid_CategoryKey.Value;
                eo.DefaultValue = this.txt_DefaultVaue.Text;
                eo.FieldType = this.ddl_FieldType.SelectedValue;
                eo.CountRule = this.ddl_CountRule.SelectedValue;
                eo.IsQueriable = Convert.ToInt32(this.ral_Queriable.SelectedValue);
                eo.IsReport = Convert.ToInt32(this.rbl_Report.SelectedValue);
                eo.RefKey = this.hid_RefCategoryKey.Value;
                try {
                    eo.CharLength = Convert.ToInt32(this.txt_Length.Text);
                } catch { }
                try {
                    eo.OrderNumber = Convert.ToInt32(this.txt_OrderNumber.Text);
                } catch { }
                biz.Update<TSysAtlEO>(eo);
                ubiz.BindPower(eo.AtlKey, this.ddl_VisiableRole.SelectedValue, this.ddl_EditableRole.SelectedValue,null, this.Actor.ID);
                BizFactory.Get<ILogBiz>().LogUpdate("属性模板", eo.AtlName, this.Actor, Request);
                Response.Redirect("List.aspx?cid=" + this.hid_CategoryKey.Value + "&t=" + AtlKey);
            }
        }
    }
}
